package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.lanyan.system.domain.vo.ClassifyVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Classify;
import com.lanyan.system.service.IClassifyService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 分类Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/classify")
public class ClassifyController extends BaseController
{
    @Autowired
    private IClassifyService classifyService;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "superior", value = "上级", dataType = "String", dataTypeClass = String.class),

    })
    public AjaxResult list(Classify classify)
    {
        List<Classify> list = classifyService.lambdaQuery()
                .eq(ClassifyVo::getDelFlag,"0")
                .isNull(ClassifyVo::getSuperior)
                .orderByAsc(ClassifyVo::getSort)
                .list();
        for (Classify item : list) {
            item.setChildren(classifyService.lambdaQuery()
                    .eq(ClassifyVo::getDelFlag,"0")
                    .eq(ClassifyVo::getSuperior,item.getClassifyId())
                    .orderByAsc(ClassifyVo::getSort)
                    .list());
        }
        return success(list);
    }

    /**
     * 导出分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classify classify)
    {
        List<Classify> list = classifyService.selectClassifyList(classify);
        ExcelUtil<Classify> util = new ExcelUtil<Classify>(Classify.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:classify:query')")
    @GetMapping(value = "/{classifyId}")
    public AjaxResult getInfo(@PathVariable("classifyId") Long classifyId)
    {
        return success(classifyService.selectClassifyByClassifyId(classifyId));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('system:classify:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "superior", value = "上级", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult add(@RequestBody Classify classify)
    {
        return toAjax(classifyService.insertClassify(classify));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('system:classify:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "superior", value = "上级", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult edit(@RequestBody Classify classify)
    {
        return toAjax(classifyService.updateClassify(classify));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('system:classify:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classifyIds}")
    public AjaxResult remove(@PathVariable Long[] classifyIds)
    {
        return toAjax(classifyService.deleteClassifyByClassifyIds(classifyIds));
    }
}
